<?php

namespace app\admin\logic;

use app\admin\library\DbInstance;
use app\admin\logic\workflow\Flow;
use app\admin\logic\workflow\Helper;
use app\admin\logic\zhxy\Business;
use app\admin\logic\zhxy\Log;
use app\admin\logic\zhxy\task\FlowHelper;
use app\admin\logic\zhxy\task\Inst;
use app\admin\logic\zhxy\task\Task;
use app\admin\logic\zhxy\task\TaskLog;
use app\admin\logic\zhxy\TaskApi;
use app\admin\model\AdminLog;
use app\common\library\Logger;
use think\Cache;
use think\Env;

/**+
 * 智慧校园 (青岛农业大学)
 *
 */
class ZHXY
{

    // 公共接收人
    const PUBLIC_USER = [ 200401061, 18153227097 ];

    /**
     * 是否开启
     *
     * @return bool
     */
    static private function getIsOpen()
    {
        return (bool)config('zhxy.isopen');
    }

    /**
     * 是否开启
     *
     * @return bool
     */
    static public function getIsSend()
    {
        return (bool)config('zhxy.issend');
    }


    /**
     * 撤回审核
     * @param $process
     * @param $row
     * @param $flow_row
     * @param $flowTable
     * @return false|mixed
     */
    static public function WorkFlowAuditRevoke($process,  $row, $flow_row, $flowTable)
    {
        if(!self::getIsOpen()){
            return true;
        }

        $logic = new Business($row);
        $logic->setFlow($flow_row, $flowTable);
        if (!$logic->isBusiness()) {
            Log::error('WorkFlowStart', [ 'no budiness', $row ]);

            return false;
        }
        $taskApi = new TaskApi();
        self::callProcess($process);

        $logic->setCurrentProcess($process);

        Logger::debug('send Reject');
        // 发送  撤回
        $res     = self::call_send($taskApi,$logic,$process,Inst::INST_STATUS_ENMU_END,Task::TASK_STATUS_RECALL);

        if (!$res[0]) {
            Log::error('send-error', $res);
            return false;
        }
        //
        Logger::debug('send .');

        // 所有的节点重置 task_id
        $all_process = \app\admin\logic\workflow\WfRunProcess::getAllProsessForPId($process->id);
        foreach ($all_process as $process117){
            $process117->task_id = self::unid();
            $process117->save();
        }
        $masterPor = \app\admin\logic\workflow\WfRunProcess::getMasterProsess($process);
        $masterPor->application_id = self::unid();
        $masterPor->save();

        return $res[0];
    }



    /**
     * 退回审核
     * @param $process
     * @param $back_run_process
     * @param $row
     * @param $flow_row
     * @param $flowTable
     * @return void
     */
    static public function WorkFlowAuditReject($process, $back_run_process, $row, $flow_row, $flowTable)
    {

//        dd($back_run_process);
        if(!self::getIsOpen()){
            return true;
        }
        $logic = new Business($row);
        $logic->setFlow($flow_row, $flowTable);
        if (!$logic->isBusiness()) {
            Log::error('WorkFlowStart', [ 'no budiness', $row ]);

            return false;
        }
        $taskApi = new TaskApi();
        self::callProcess($process);


        $logic->setCurrentProcess($process);

        Logger::debug('sendAuditReject ');
        // 发送  退回
        $res     = self::call_send($taskApi,$logic,$process,Inst::INST_STATUS_ENMU_END,Task::TASK_STATUS_REJECT);
        if (!$res[0]) {
            Log::error('send-error', $res);
            return false;
        }
        //设置重新开始的节点的新的task_id
        $all_process = \app\admin\logic\workflow\WfRunProcess::getAllProsessForPId($process->id);
        foreach ($all_process as $process117){
            if(!$back_run_process || $process117->run_flow_process >= $back_run_process->run_flow_process){
                $process117->task_id = self::unid();
                $process117->save();
            }
            if($back_run_process->id == $process117->id){
                $back_run_process = $process117;
            }
        }

        // 发送 重新开始
        Logger::debug('sendAuditReject-back_run_process');

        if($back_run_process){
            self::callProcess($back_run_process);

            $res     = self::call_send($taskApi,$logic,$back_run_process,Inst::INST_STATUS_ENMU_RUN,Task::TASK_STATUS_RUN);

            if (!$res[0]) {
                Log::error('send-error', $res);
                return false;
            }
        }else{
            $masterPor             = \app\admin\logic\workflow\WfRunProcess::getMasterProsess($process);

            if (!$masterPor->application_id) {
                $masterPor->application_id = ZHXY::unid();
                $masterPor->save();
            }
        }
        Logger::debug('send .');
        return $res[0];
    }

    static public function unid()
    {
        return uniqid('cggl') . mt_rand(100000, 999999);
    }

    /**
     * 审核完成
     * @param $process
     * @param $row
     * @param $flow_row
     * @param $flowTable
     * @return void
     */
    static public function WorkFlowAuditCompleted($process, $row, $flow_row, $flowTable)
    {
        if(!self::getIsOpen()){
            return true;
        }
        $logic = new Business($row);
        $logic->setFlow($flow_row, $flowTable);
        if (!$logic->isBusiness()) {
            Log::error('WorkFlowStart', [ 'no budiness', $row ]);

            return false;
        }
        $taskApi = new TaskApi();
        self::callProcess($process);

        $logic->setCurrentProcess($process);

        // 发送OK
        $res     = self::call_send($taskApi,$logic,$process,Inst::INST_STATUS_ENMU_OK,Task::TASK_STATUS_OK);

        if (!$res[0]) {
            Log::error('send-error', $res);
        }

        return $res[0];
    }


    /***
     * 审核开始
     *
     * @param \app\admin\model\WfRunProcess $process 当前进度
     * @param \app\admin\model\Intention|\app\admin\model\Projectlib|object $row 业务数据
     * @param \app\admin\model\Flow $flow_row
     * @param \app\admin\model\FlowTable $flowTable
     *
     * @return void
     */
    static public function WorkFlowStart($process = null, $row = null, $flow_row = null, $flowTable = null)
    {
        if(!self::getIsOpen()){
            return true;
        }

        $logic = new Business($row);
        $logic->setFlow($flow_row, $flowTable);
        if (!$logic->isBusiness()) {
            Log::error('WorkFlowStart', [ 'no budiness', $row ]);
            throw new \Exception('WorkFlowStart no budiness ' );
            return false;
        }
        self::callProcess($process);
        $logic->setCurrentProcess($process);

        $taskApi = new TaskApi();
        $res     = self::call_send($taskApi,$logic,$process,Inst::INST_STATUS_ENMU_RUN,Task::TASK_STATUS_RUN);
        if (!$res[0]) {
            Log::error('send-error', $res);
            return false;
        }

        return $res[0];

    }


    /**
     * 审核通过 (还有下一步)
     *
     * @param \app\admin\model\WfRunProcess $process
     * @param \app\admin\model\WfRunProcess $current_next
     * @param $row
     * @param $flow_row
     * @param $flowTable
     * @return false|mixed
     */
    static public function  WorkFlowAuditPass($process, $current_next, $row, $flow_row, $flowTable)
    {
        if(!self::getIsOpen()){
            return true;
        }
        $logic = new Business($row);
        $logic->setFlow($flow_row, $flowTable);
        if (!$logic->isBusiness()) {
            Log::error('WorkFlowStart', [ 'no budiness', $row ]);

            return false;
        }
        $taskApi = new TaskApi();
        self::callProcess($process);
        self::callProcess($current_next);

        $logic->setCurrentProcess($process);
        $logic->next_process = $current_next;


        // 发送OK

        $res     = self::call_send($taskApi,$logic,$process,Inst::INST_STATUS_ENMU_RUN,Task::TASK_STATUS_OK);

        if (!$res[0]) {
            Log::error('send-error', $res);
            return false;
        }
        // 发送 run 下一步的
        $res     = self::call_send($taskApi,$logic,$current_next,Inst::INST_STATUS_ENMU_RUN,Task::TASK_STATUS_RUN);

        if (!$res[0]) {
            Log::error('send-error2', $res);
            return false;
        }

        return $res[0];

    }

    /**
     * 处理 节点
     * @param \app\admin\model\WfRunProcess $process
     * @return void
     */
    static private function callProcess(&$process)
    {
        $process->refresh();

        if (!$process->task_id) {
            Log::error('callProcess- task_id', $process->task_id);
            $process->task_id = ZHXY::unid();
            $process->save();
        }

    }

    /**
     * 进行发送
     * @param TaskApi $taskApi
     * @param Business\AbstractBusiness $logic
     * @param \app\admin\model\WfRunProcess $process
     * @param $instStatus
     * @param $taskStatus
     * @return mixed
     */
    static private function call_send($taskApi,$logic, $process , $instStatus = Inst::INST_STATUS_ENMU_OK,$taskStatus =Task::TASK_STATUS_OK)
    {

        $inst = $logic->createInst($process, $instStatus);
        $task = $logic->createTask($process, $taskStatus);

        $appname = $logic->getAppName();


        return $taskApi->push_task($inst, $task, $appname);

    }









}